package com.exit66.jukebox.tag;

import java.io.*;
//import org.jd3lib.*;
import helliker.id3.*;


/**
 * This class gives information (audio format and comments) about MPEG file.
 */
public class ID3Reader extends TagReader
{
	   private static final String[] id3v1Tags = {
               "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal",
               "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative",
               "Ska", "Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
               "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
               "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock", "Ethnic",
               "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock",
               "Comedy", "Cult", "Gangsta", "Top", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
               "New Wave", "Psychadelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
               "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing",
               "Fast Fusion", "Bebob", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
               "Prog. Rock", "Psychedel. Rock", "Symph. Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening",
               "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
               "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
               "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "Acapella", "Euro-House", "Dance Hall"};
	   
	public ID3Reader(String fileName) {
		super(fileName);
	}

	public boolean readTag() {
		try {
			File file = new File(_fileName);
			if (!file.exists()) 
				return false;
			MP3File mp3file = new MP3File(file);
			if (mp3file.id3v1Exists() || mp3file.id3v2Exists()) {
				_artistName = mp3file.getArtist();
				_albumName = mp3file.getAlbum();
				_trackName = mp3file.getTitle();
				_trackNumber = mp3file.getTrack();
				_contentType = mp3file.getGenre();
				if ((_contentType.length() >= 3) && (_contentType.charAt(0) == '(') && (_contentType.charAt(_contentType.length() - 1) == ')')) {
					try {
						int id = Integer.parseInt(_contentType.substring(1, _contentType.length() - 1));
						if (id < id3v1Tags.length) {
							_contentType = id3v1Tags[id];
						}
					}
					catch (Exception e) {
						System.err.println(e);
					}
				}
				return true;
			}
		}
		catch (Exception e) {
			System.err.println(e);
		}
		return false;
	}
	
	public boolean loadCoverImage() {
		ByteArrayInputStream input;
		_coverImage = new ByteArrayOutputStream();
		
		try {
			File file = new File(_fileName);
			if (!file.exists()) 
				return false;
			MP3File mp3file = new MP3File(file);
			if (mp3file.id3v2Exists()) {
				input = new ByteArrayInputStream(mp3file.getFrameDataBytes("APIC"));
				
                byte x[] = new byte[1];
                byte mimeBuf[] = new byte[50];
                byte buf[] = new byte[1024];
                int len;

                int j;

                // image type
                input.skip(1);
                // read in the mime type
                input.read(x);

                for (j=0; j < input.available() && x[0] != 0; j++) {
                    mimeBuf[j] = x[0];
                    input.read(x, 0, 1);

                }
                
                _coverMimeType = new String(mimeBuf, 0, j);

                // picture type flag
                input.skip(1);
                
                // skip description
                input.read(x);
                for (j=0; j < input.available() && x[0] != 0; j++) {
                    input.read(x, 0, 1);

                }
                
                do {
                	len = input.read(buf, 0, 1024);
                	if (len != -1)
                		_coverImage.write(buf, 0, len);
                } while (len != -1); 
                
				return true;
			}
		}
		catch (Exception e) {
			System.err.println(e);
		}
		
		return false;

	}
	
}